<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台-志愿者风采</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/kindeditor-4.1.10/themes/default/default.css" rel="stylesheet" type="text/css" />
</head>
<style>
#mienContent {
	text-align: center;
}

#mienContent img {
	width: 60%;
	height: auto;
	margin: 20px;
}
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<!-- 							<th class="text-center" data-field=""></th> -->
							<th class="text-center" data-field="project">项目名称</th>
							<th class="text-center" data-field="person">负责人</th>
							<th class="text-center" data-field="tel">联系电话</th>
							<th class="text-center" data-field="addr">地址</th>
							<th class="text-center" data-field="createTime">发布时间</th>
							<th class="text-center" data-field="vmId">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade bs-example-modal-lg" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">找居委会详情</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="project" class="control-label">项目名称:</label> <input type="text" class="form-control" id="project">
					</div>
					<div class="form-group">
						<label for="recruit_info_theme" class="control-label">标题图片(124 x 124):</label>
						<button id="imgBtn" class="btn btn-primary glyphicon glyphicon-plus" onclick="imgFile.click();">上传图片</button>
						<img id="image" class="cover-radius" width="150px;" style="cursor: pointer;" onclick="imgFile.click();" /> <input type="file" class="form-control" style="display: none;" name="imgFile" id="imgFile" onchange="fileChange()">
					</div>
					<div class="form-group">
						<label for="person" class="control-label">负责人:</label> <input type="text" class="form-control" id="person">
					</div>
					<div class="form-group">
						<label for="tel" class="control-label">联系电话:</label> <input type="text" class="form-control" id="tel">
					</div>
					<div class="form-group">
						<label for="addr" class="control-label">地址:</label> <input type="text" class="form-control" id="addr">
					</div>
					<div class="form-group">
						<label for="notice" class="control-label">预约须知:</label>
						<textarea id="notice" style="width: 100%; height: 100px;"></textarea>
					</div>
					<div class="form-group">
						<label for="notice" class="control-label">办理流程:</label>
						<textarea style="width: 100%; height: 500px;" class="form-control" rows="30" id="column1_textarea" name="kindEditor_content"></textarea>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id="submitBtn" type="button" class="btn btn-primary" onclick="submitMien();">提交</button>
				</div>
			</div>
		</div>
	</div>

</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/plugins/laydate/laydate.js"></script>
<!-- kindeditor -->
<script src="${pageContext.request.contextPath}/kindeditor-4.1.10/kindeditor-min.js"></script>
<script src="${pageContext.request.contextPath}/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<script>
	var editor;
	var selectId = "";
	var imgUrl = "";
	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="kindEditor_content"]', {
			items : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter',
					'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|',
					'fullscreen', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|',
					'image', 'multiimage', 'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ], //配置kindeditor编辑器的工具栏菜单项
			cssPath : 'kindeditor-4.1.10/plugins/code/prettify.css',
			allowFileManager : true,
			uploadJson : '${pageContext.request.contextPath}/upload_img.do', //上传
			fileManagerJson : '${pageContext.request.contextPath}/mien_file_manager.do', // 文件管理
			filterMode : true, //是否启用过滤模式(编辑文本框中是否可以出现控件显示)
			htmlTags : {
				span : [ '.color', '.background-color', '.font-size', '.font-family', '.background', '.font-weight', '.font-style', '.text-decoration', '.vertical-align', '.line-height' ],
				font : [ 'id', 'class', 'color', 'size', 'face', '.background-color' ],
				a : [ 'id', 'class', 'href', 'target', 'name' ],
				embed : [ 'id', 'class', 'src', 'width', 'height', 'type', 'loop', 'autostart', 'quality', '.width', '.height', 'align', 'allowscriptaccess' ],
				img : [ 'id', 'class', 'src', 'width', 'height', 'border', 'alt', 'title', 'align', '.width', '.height', '.border' ],
				'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [ 'id', 'class', 'align', '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.background', '.font-weight',
						'.font-style', '.text-decoration', '.vertical-align', '.text-indent', '.margin-left' ],
				pre : [ 'id', 'class' ],
				hr : [ 'id', 'class', '.page-break-after' ],
				'br,tbody,tr,strong,b,sub,sup,em,i,u,strike,s,del' : [ 'id', 'class' ],
				iframe : [ 'id', 'class', 'src', 'frameborder', 'width', 'height', '.width', '.height' ]
			},
			allowFileManager : true,
			allowUpload : false, //允许上传图片
			imageUploadJson : '/saveImg', //服务端上传图片处理URI
			resizeType : 0,//使其不可以拖动 
			afterCreate : function() {
				var self = this;
				K.ctrl(document, 13, function() {
					self.sync();
				});
				K.ctrl(self.edit.doc, 13, function() {
					self.sync();
				});
			},
			afterBlur : function() {
				this.sync();
			}
		});
	});

	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-success btn-xs glyphicon glyphicon-file' onclick='showModal(\"" + row.id + "\");'>查看详情</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='del(\"" + row.id + "\")'>删除信息</button>", ].join('');
	}
	var table_dataset = [ {
		field : 'project',
		align : 'center',
	}, {
		field : 'person',
		align : 'center',
	}, {
		field : 'tel',
		align : 'center',
	}, {
		field : 'addr',
		align : 'center',
	}, {
		field : 'createTime',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return (new Date(row.createTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable(
			{
				url : "${pageContext.request.contextPath}/selectCommittees",
				idField : "vmId",
				columns : table_dataset,
				striped : true, //是否显示行间隔色
				pagination : true, //是否显示分页
				pageSize : 10, //每页的记录行数
				pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
				search : false, //是否显示搜索
				showRefresh : true,
				sidePagination : "server", //表示服务端请求  
				queryParamsType : "limit",
				queryParams : function queryParams(params) { //设置查询参数  
					var param = {
						offset : params.offset,
						limit : params.limit,
						name : $("#name").val()
					};
					return param;
				},
				toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>"
						+ "<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'>"
						+ "<input type='text' class='form-control' placeholder='请输入预约账号进行查询...' id='name' name='name'>"
						+ "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='Querry();'>查询</button></span></div>",
				icons : {
					paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
					paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
					refresh : 'glyphicon-refresh icon-refresh',
					toggle : 'glyphicon-list-alt icon-list-alt',
					columns : 'glyphicon-th icon-th',
					detailOpen : 'glyphicon-plus icon-plus',
					detailClose : 'glyphicon-minus icon-minus',
					"export" : 'glyphicon-export icon-share'
				},
				showExport : true,
				exportDataType : 'basic',
				exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
			});

	// 	$(function() {
	// 		$("#image").hide();
	// 	});

	function showModal(id) {
		selectId = id;
		var element = document.getElementById('image');
		if (id == '') {
			$("#project").val('');
			$("#person").val('');
			$("#tel").val('');
			$("#addr").val('');
			$("#notice").val('');
			element.src = "";
			imgUrl = "";
			editor.html('');
			$("#image").hide();
			$("#imgBtn").show();
		} else {
			$.ajax({
				url : "${pageContext.request.contextPath}/selectCommitteesById",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					var committee = data['committee'];
					$("#project").val(committee.project);
					$("#person").val(committee.person);
					$("#tel").val(committee.tel);
					$("#addr").val(committee.addr);
					$("#notice").val(committee.notice);
					$("#column1_textarea").val(committee.process);
					element.src = "../upload/zhsq_xg/" + committee.picture;
					editor.html(committee.process);
					$("#image").show();
					$("#imgBtn").hide();
					imgUrl = committee.picture;
				}
			});
		}
		$('#modal').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function submitMien() {
		$("#submitBtn").button('loading');
		var project = $("#project").val();
		var person = $("#person").val();
		var tel = $("#tel").val();
		var addr = $("#addr").val();
		var notice = $("#notice").val();
		var process = $('#column1_textarea').val();
		var url = imgUrl;
		if (project == '' || person == '' || tel == '' || addr == '' || notice == '' || process == '' || url == '') {
			layer.alert("数据填写不完整!");
			$("#submitBtn").button('reset');
			return;
		}
		if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(tel))) {
			layer.alert("联系方式格式不正确！");
			$("#submitBtn").button('reset');
			return false;
		}
		if(editor.isEmpty()){
			layer.alert("数据填写不完整!");
			$("#submitBtn").button('reset');
			return false;
		}
		$.ajax({
			url : "${pageContext.request.contextPath}/insertAndUpdateCommittee",
			type : "post",
			data : {
				id : selectId,
				project : project,
				person : person,
				tel : tel,
				addr : addr,
				notice : notice,
				process : process,
				picture : url
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
					}, 1000);
					$("#modal").modal("hide");
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
				$("#submitBtn").button('reset');
			}
		})
	}

	function fileChange() {
		//document.getElementById("upload_file_img").value = document.getElementById("imgFile").value;
		if (image_check('imgFile')) { //自己添加的文件后缀名的验证
			$.ajaxFileUpload({
				fileElementId : 'imgFile', //需要上传的文件域的ID，即<input type="file">的ID。
				url : "${pageContext.request.contextPath}/upload_img", //后台方法的路径
				type : 'post', //当要提交自定义参数时，这个参数要设置成post
				dataType : 'json', //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
				secureuri : false, //是否启用安全提交，默认为false。
				async : true, //是否是异步
				success : function(data) { //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
					imgUrl = data.fileName;
					$('#image').attr('src', '${pageContext.request.contextPath}/' + data.url);
					$("#image").show();
					$("#imgBtn").hide();
				}
			});
		}
	}

	function image_check(feid) { //自己添加的文件后缀名的验证
		var img = document.getElementById(feid);
		return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function() {
			layer.alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
			return false;
		})();
	}

	function Querry() {
		$("#table").bootstrapTable('refresh');
	}
	
	function del(id) {
		layer.confirm('确认删除吗？', function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/updateCommittee",
				type : "post",
				data : {
					id : id,
					s1 : '-1'
				},
				dataType : "json",
				success : function(data) {
					var result = data['result'];
					if (result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 1000);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				}
			});
		});
	}
</script>
</html>
